package com.java.controller;

import com.java.pojo.Page;
import com.java.service.EnterGoodOkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/EnterGoodOk")
public class EnterGoodOk {

    @Autowired
    private EnterGoodOkService enterGoodOkService;

    @RequestMapping(value = "/login", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String s(){
        return "<script>alert('欢迎回来');location.href='/EnterGoodOk/findAll';</script>";
    }


    /*查询没有退货*/
    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "start",required = false) Integer start,
                          @RequestParam(value = "document2",required = false) String document2,
                          Model model){
        if(document2==null ||"".equals(document2)){
            document2="";
        }

        if(start==null ||start.equals("")){
            start=1;
        }
        System.out.println(start+"+"+document2);
        /*计算总个数*/
        Integer count = enterGoodOkService.getCount(document2,1);
        int i = count % 4 ==0? count/4 :(count/4)+1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if(start>=i){
            start=i;
        }else if(start<=1){
            start=1;
        }

        Page<com.java.pojo.EnterGoodOk> page = enterGoodOkService.getPageByPage(start, document2);
        System.out.println("page:"+page);
        if(page==null){
            return "error";
        }else {
            model.addAttribute("page",page);
            model.addAttribute("document",document2);
            return "enterGoodOk/findAllByPage";
        }
    }


    /*查询退货*/
    @RequestMapping("/findAll2")
    public String findAll2(@RequestParam(value = "start",required = false) Integer start,
                          @RequestParam(value = "document2",required = false) String document2,
                          Model model){
        if(document2==null ||"".equals(document2)){
            document2="";
        }

        if(start==null ||start.equals("")){
            start=1;
        }
        System.out.println(start+"+"+document2);
        /*计算总个数*/
        Integer count = enterGoodOkService.getCount(document2,0);
        int i = count % 4 ==0? count/4 :(count/4)+1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if(start>=i){
            start=i;
        }else if(start<=1){
            start=1;
        }

        Page<com.java.pojo.EnterGoodOk> page = enterGoodOkService.getPageByPage2(start, document2);
        System.out.println("page:"+page);
        if(page!=null) {
            model.addAttribute("page", page);
            model.addAttribute("document", document2);
            return "enterGoodOk/findAllByPage2";
        }else {
            model.addAttribute("msg","无信息!");
            return "enterGoodOk/findAllByPage2";
        }
    }


    /*删除*/
    @RequestMapping(value = "/del",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String del(@RequestParam(value = "eid",required = false) String eid){
        System.out.println(eid+":----");
        int del = enterGoodOkService.del(Integer.parseInt(eid));
        if(del>0){
            return "<script>alert('欢迎回来');location.href='/EnterGood/EnterPageGood';</script>";
        }
        return "error";
    }
}
